package com.jied.duodian.web.qt;

import com.jied.duodian.config.MinioHelper;
import com.jied.duodian.util.CommonResult;
import com.jied.duodian.util.SuId;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Description:
 *
 * @author LiJie
 * @date Created on 2020/11/6
 * WWW https://github.com/j6l
 */
@RestController
@RequestMapping("/file")
@Slf4j
@CrossOrigin
public class FileUploadController {

    @Autowired
    MinioHelper minioHelper;

    /**
     * 实现文件上传
     */
    @RequestMapping("/fileUpload")
    @ResponseBody
    public String fileUpload(@RequestParam("file") MultipartFile file) {
        String s = "";
        try {
            s = minioHelper.putObject( file );
            return s;
        } catch (InvalidEndpointException e) {
            e.printStackTrace();
        } catch (InvalidPortException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (RegionConflictException e) {
            e.printStackTrace();
        }

        return "上传失败!";
    }

    // if (file.isEmpty()) {
    //     return CommonResult.failed();
    // }
    // String fileName = file.getOriginalFilename();
    // int size = (int) file.getSize();
    // System.out.println( fileName + "-->" + size );
    // // ApplicationHome h = new ApplicationHome( getClass() );
    // // String dirPath = h.getSource().getParentFile().toString();
    // // String path = Thread.currentThread().getContextClassLoader().getResource( "" ).getPath() + "static";
    // // String path = ResourceUtils.getURL( "classpath:" ).getPath() + "static";;
    // String path = System.getProperty( "user.dir" ) + "\\src\\main\\resources\\static\\img";
    // String fileName1 = getNewFileName( fileName );
    // System.out.println("dirPath  ==>"+ path);
    // File dest = new File( path + "/" + fileName1 );
    // if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
    //     dest.getParentFile().mkdir();
    // }
    // try {
    //     file.transferTo( dest ); //保存文件
    //     return CommonResult.success( "/img/"+fileName1 );
    // } catch (IllegalStateException e) {
    //     // TODO Auto-generated catch block
    //     e.printStackTrace();
    //     return CommonResult.failed();
    // } catch (IOException e) {
    //     // TODO Auto-generated catch block
    //     e.printStackTrace();
    //     return CommonResult.failed();
    // }
    // }


}
